package com.uujava.minibox.gui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;

import com.mxcyk.serviceLogic.Appfile;
import com.mxcyk.serviceLogic.FileList;
import com.mxcyk.serviceLogic.MakeFileLise;
import com.mxcyk.serviceLogic.Run;
import com.uujava.minibox.config.Config;
import com.uujava.minibox.ui.cartoon.pulse.Pulse;

import tutorial.SplitPagePanel;

/**功能：返回一个填充好的滚动应用列表
 * @author k 2013年8月5日 12:38:22
 * 
 */
public class ScrolledApp implements MouseListener {
	SplitPagePanel scrolled = null;//滚动面板
	String path=null;//文件存放目录
	Pulse pulse = new Pulse();//脉动动画
	Border border=null;
	/**
	 * @param path
	 *            App存放的文件路径
	 * @param xy_axis
	 *            滚动App条的方向（垂直or水平）
	 */
	public ScrolledApp(String path, int xy_axis) {
		scrolled = new SplitPagePanel(xy_axis);// 设置好面板内容的方向
		
		this.path=path;
		FileList getapp = new FileList(path);
		MakeFileLise mfl = getapp.getApp();
		ArrayList<Appfile> appfile = mfl.getLs();
		for (int size = appfile.size(), i = 0; i < size; i++) {
			scrolled.addButton(addApp(appfile.get(i)));
		}
		
	}
	public void print(String str) {
		System.out.println(str);
	}
	// 返回滚动panel
	public SplitPagePanel getSplitPagePanel() {
		return scrolled;
	}
	// 工厂方法
	private JLabel addApp(Appfile app) {
		JLabel lab = new JLabel(app.getSmallIcon());
		lab.setName(app.getAppName());
		lab.addMouseListener(this);
		lab.setToolTipText(app.getAppName());
		if(border==null)
		border=lab.getBorder();
		return lab;
	}

	// =========================================

	// =========================================

	public static void main(String[] args) {

		JFrame jf = new JFrame();
		jf.setBackground(new Color(100));
		ScrolledApp line = new ScrolledApp(Config.PLUGINS + "上",
				BoxLayout.X_AXIS);
		System.out.println(Config.PLUGINS + "上");
		line.scrolled.setPageResultSet(line.scrolled.getButtons());
		jf.add(line.scrolled);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设定窗体关闭后自动退出进程
		jf.setSize(600, 60);
		jf.setVisible(true);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		String name = ((JLabel) e.getSource()).getName();// 得到的名字
		StringBuffer str=new StringBuffer(path).append(Config.SEPARATOR).append(name);
		Run.getRun().run(new File(str.toString()));
		print(str.toString());
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}
